package com.atguigu.gulimall.order.config;

import com.atguigu.common.constant.OrderConstant;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author baiWT
 * @Description
 * @Date 2022/4/11 17:39
 **/
@Configuration
public class MyMQConfig {

//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
//        System.out.println("收到过期订单信息："+orderEntity.getOrderSn());
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
//    }

    /**
     * 容器中的binding、queue、exchange都会自动创建(rabbitMQ没有的情况)
     * 如果rabbitMQ中有，那么就算改变声明的属性也不会覆盖
     * 死信队列/延时队列
     * x-dead-letter-exchange="stock-event-exchange"
     * x-dead-letter-routing-key="stock.release"
     * x-message-ttl="60000"
     */
    @Bean
    public Queue orderDelayQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        // 			@Nullable Map<String, Object> arguments
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000);
        return new Queue("order.delay.queue",
                true,
                false,
                false,
                arguments);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        // 			@Nullable Map<String, Object> arguments
        return new Queue("order.release.order.queue",
                true,
                false,
                false);
    }

    @Bean
    public Exchange orderEventExchange() {
        // String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange("order-event-exchange",
                true,
                false);
    }

    /**
     * 绑定关系
     */
    @Bean
    public Binding orderCreateBinding() {
        // String destination, DestinationType destinationType, String exchange, String routingKey,
        // 			@Nullable Map<String, Object> arguments
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseOrderBind() {
        // String destination, DestinationType destinationType, String exchange, String routingKey,
        // 			@Nullable Map<String, Object> arguments
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    @Bean
    public Queue orderSeckillOrderQueue() {
        //String name, boolean durable, boolean exclusive, boolean autoDelete,
        // 			@Nullable Map<String, Object> arguments
        return new Queue(OrderConstant.ORDER_SECKILL_DEAL_QUEUE,
                true,
                false,
                false);
    }

    @Bean
    public Binding stockReleaseBinding() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

    /**
     * 秒杀消息队列和交换机的绑定关系
     * @return
     */
    @Bean
    public Binding seckillOrderDealBinding() {
        // String destination, DestinationType destinationType, String exchange, String routingKey,
        // 			@Nullable Map<String, Object> arguments
        return new Binding(OrderConstant.ORDER_SECKILL_DEAL_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderConstant.ORDER_EVENT_EXCHANGE,
                OrderConstant.ORDER_SECKILL_DEAL_QUEUE_ROUTING_KEY,
                null);
    }
}
